﻿using System;
using System.Collections.Generic;
using System.Text;
using Avalonia.Collections;
using Avalonia.VisualTree;

namespace AvaloniaApplication1.Helper
{

    /// <summary>
    /// 视图帮助
    /// </summary>
    public class VisualHelp
    {
        /// <summary>
        /// 获取所有的控件
        /// </summary>
        /// <param name="parent"></param>
        /// <returns></returns>
        public static IAvaloniaList<IVisual> GetChildrens(IVisual parent)
        {
            IAvaloniaList<IVisual> lisChildrens = new AvaloniaList<IVisual>();
            foreach (IVisual visual in parent.VisualChildren)
            {
                lisChildrens.Add(visual);
                if (visual.VisualChildren.Count > 0)
                {
                    IAvaloniaList<IVisual> visuals = GetChildrens(visual);
                    lisChildrens.AddRange(visuals);
                }
            }
            return lisChildrens;
        }

        public static IAvaloniaList<T> GetChildrens<T>(IVisual parent) where T : IVisual
        {
            IAvaloniaList<T> lisChildrens = new AvaloniaList<T>();
            foreach (IVisual visual in parent.VisualChildren)
            {
                if (visual is T t)
                {
                    lisChildrens.Add(t);
                }
                if (visual.VisualChildren.Count > 0)
                {
                    IAvaloniaList<T> visuals = GetChildrens<T>(visual);
                    lisChildrens.AddRange(visuals);
                }
            }
            return lisChildrens;
        }
    }
}
